﻿using System;
using System.Collections.Generic;
using System.Text;
using Microsoft.Xna.Framework;
using System.Collections;
using JEng_Mark_1.Interfaces;
using Microsoft.Xna.Framework.Graphics;
using System.Threading;
using JEng_Mark_1.GameComponents;

namespace JEng_Mark_1.Managers
{
    public class JModelManager : GameComponent
    {
        private static Dictionary<string, IJEngModel> _textures = new Dictionary<string, IJEngModel>();

        private static bool _initialized = false;
        /// <summary>
        /// Is the ModelManagers Initialized, used for test cases and setup of Effects.
        /// </summary>
        public static bool Initialized
        {
            get { return _initialized; }
        }

        private static int _modelsLoaded = 0;
        /// <summary>
        /// The number of textures that are currently loaded.
        /// Use this for user loading bar feedback.
        /// </summary>
        public static int ModelsLoaded
        {
            get { return _modelsLoaded; }
        }

        /// <summary>
        /// Create the shader Managers.
        /// </summary>
        /// <param name="game"></param>
        public JModelManager(Game game)
            : base(game)
        {
        }

        /// <summary>
        /// Add a shader of type RoeModel.
        /// </summary>
        /// <param name="newModel"></param>
        /// <param name="textureName"></param>
        public static void AddModel(IJEngModel newModel, string textureName)
        {
            if (textureName != null && !_textures.ContainsKey(textureName))
            {
                _textures.Add(textureName, newModel);
                if (_initialized)
                {
                    ThreadStart threadStarter = delegate
                    {
                        newModel.LoadContent();
                        _modelsLoaded++;
                        JLog.addMessage("Added model " + newModel.FileName + " total models = " + _modelsLoaded);
                    };
                    Thread loadingThread = new Thread(threadStarter);
                    loadingThread.Start();
                }
            }
        }

        /// <summary>
        /// Get a texture
        /// </summary>
        /// <param name="textureId"></param>
        /// <returns></returns>
        public static IJEngModel GetModel(string textureName)
        {
            if (textureName != null && _textures.ContainsKey(textureName))
            {
                return _textures[textureName];
            }
            return null;
        }

        /// <summary>
        /// Create the shaders.
        /// </summary>
        public override void Initialize()
        {
            base.Initialize();

            ThreadStart threadStarter = delegate
            {
                foreach (IJEngModel texture in _textures.Values)
                {
                    if (!texture.ReadyToRender)
                    {
                        texture.LoadContent();
                        _modelsLoaded++;
                    }
                }
            };
            Thread loadingThread = new Thread(threadStarter);
            loadingThread.Start();

            _initialized = true;
        }
    }
}
